package com.sxt.bus.service.imp;

import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Inport;
import com.sxt.bus.domain.Sales;
import com.sxt.bus.mapper.GoodsMapper;
import com.sxt.bus.mapper.SalesMapper;
import com.sxt.bus.service.SalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2019-12-26
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public boolean save(Sales entity) {
        //根据商品编号查询商品
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        goods.setNumber(goods.getNumber()-entity.getNumber());
        goodsMapper.updateById(goods);
        //保存信息
        return super.save(entity);
    }

    @Override
    public boolean updateById(Sales entity) {
        //根据进货ID查询进货
        Sales sales = this.baseMapper.selectById(entity.getId());
        //根据商品编号查询商品库存
        Goods goods =this.goodsMapper.selectById(entity.getGoodsid());
        //库存算法,当前库存+销售单修改之前的数量-修改之后的数量
        goods.setNumber(goods.getNumber()+sales.getNumber()-entity.getNumber());
        //更新商品库存
        this.goodsMapper.updateById(goods);
        //更新进货单
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        //根据进货ID查询进货
        Sales sales = this.baseMapper.selectById(id);
        //根据商品编号查询商品库存
        Goods goods =this.goodsMapper.selectById(sales.getGoodsid());
        //库存算法,当前库存+销售单的数量
        goods.setNumber(goods.getNumber()+sales.getNumber());
        //更新商品库存
        this.goodsMapper.updateById(goods);
        return super.removeById(id);
    }

}
